package com.suibibk.springCloud.order.service.impl;

import com.suibibk.springCloud.order.mapper.OrderMapper;
import com.suibibk.springCloud.order.openfeign.StockService;
import com.suibibk.springCloud.order.pojo.Order;
import com.suibibk.springCloud.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    StockService stockService;
    @Autowired
    OrderMapper orderMapper;
    @GlobalTransactional
    @Trace
    @Tags({@Tag(key="add",value="returnedObj"),
        @Tag(key="add",value = "arg[0]")})
    @Override
    public String add(Long merchId) throws InterruptedException {
        System.out.println("新增订单开始"+merchId);
        Order order = new Order();
        Random rd=new Random();
        long l1=(int)(Math.random()*9000+1000);
        order.setOrderNo(l1);
        order.setOrderName("新增订单，对应商品"+merchId);
        orderMapper.insert(order);
        System.out.println("新增订单结束"+merchId);
        //新增订单
        stockService.reduce(1l);
        System.out.println("扣减库存结束"+merchId);
       // int i =1/0;
        Thread.sleep(2000);
        return "我是返回";
    }
}
